package com.yy.json;

import java.util.List;

public class ListSerializer implements ObjectSerializer {
    public static final ListSerializer instance = new ListSerializer();

    @Override
    public void serializer(JsonConfig config, StringBuilder out, Object object) {
        List<?> list = (List<?>) object;
        if (list.isEmpty()) {
            out.append("[]");
            return;
        }
        out.append("[");
        // int i = 0;

        for (int i = 0; i < list.size(); i++) {
            if (i != 0) {
                out.append(",");
            }
            Object item = list.get(i);
            if (item == null) {
                out.append("null");
            } else {
                //TODO
                Class<?> aClass = item.getClass();
                if (Utils.isBox(aClass)) {
                    out.append(item);
                } else if (Utils.isString(aClass)) {
                    out.append("\"");
                    out.append(item);
                    out.append("\"");
                } else {
                    ObjectSerializer serializer = config.getSerializer(aClass);
                    serializer.serializer(config, out, item);
                }
            }

        }

        out.append("]");
    }
}
